package cn.bgs.vo;

import cn.bgs.mapper.UserMapper;
import cn.bgs.utils.CommonResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Autowired
    UserMapper userMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if(request.getMethod().equals("OPTIONS")){
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }
        if(!StringUtils.isEmpty(token)){
            //获取token是否存在
            TokenPage i= userMapper.getTokenTime(token);
            if (i!=null){
                //获取当前时间
                Date data=new Date();
                //将当前时间毫秒化
                long nowTime=data.getTime();
                //获取token最后调用时间
                Date updateDame=i.getUpdateDate();
                //将 token最后调用时间 毫秒化
                long tokenUpdateDame=updateDame.getTime();
                //计算距离上次调用token时间差
                long time=nowTime/60000-tokenUpdateDame/60000;
                if (time<30){
                    userMapper.updateTokenTime(token);
                    return true;
                }else {
                    response.setStatus(HttpStatus.OK.value());
                    response.setCharacterEncoding("UTF-8");
                    response.setHeader("Content-Type", "application/json;charset=UTF-8");
                    response.setHeader("Access-Control-Allow-Origin", "*");
                    PrintWriter out = null;
                    try {
                        out = response.getWriter();
                        out.append(JSONObject.toJSONString(CommonResult.error(402,"已超时，请重新登录！")));
                        out.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return false;
                }
            }else{
                response.setStatus(HttpStatus.OK.value());
                response.setCharacterEncoding("UTF-8");
                response.setHeader("Content-Type", "application/json;charset=UTF-8");
                response.setHeader("Access-Control-Allow-Origin", "*");
                PrintWriter out = null;
                try {
                    out = response.getWriter();
                    out.append(JSONObject.toJSONString(CommonResult.error(402,"token为空非法闯入！" )));
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return false;
            }
        }else{
            response.setStatus(HttpStatus.OK.value());
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            response.setHeader("Access-Control-Allow-Origin", "*");
            PrintWriter out = null;
            try {
                out = response.getWriter();
                out.append(JSONObject.toJSONString(CommonResult.error(402,"shibai" )));
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}
